package br.com.dados;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	ImageView image_view;
	Button btnLoadImg;

	final static String imageLocation = "http://192.168.1.125:8080/Transferencia_arquivos_jersey_IMG/imagem/get";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		image_view = (ImageView) findViewById(R.id.imageview);
		btnLoadImg = (Button) findViewById(R.id.btn_imgload);

		btnLoadImg.setOnClickListener(loadImage);
	}

	View.OnClickListener loadImage = new View.OnClickListener() {
		public void onClick(View view) {
			loadImage(imageLocation);
		}
	};
	Bitmap bitmap;

	void loadImage(String image_location) {

		URL imageURL = null;

		try {
			imageURL = new URL(image_location);
		}

		catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			HttpURLConnection connection = (HttpURLConnection) imageURL.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream inputStream = connection.getInputStream();
			bitmap = BitmapFactory.decodeStream(inputStream);// Converter Para Bitmap
			image_view.setImageBitmap(bitmap);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
